package com.gengzp.system.controller;

import com.gengzp.common.annotations.OperationLogIgnore;
import com.gengzp.common.model.generic.req.SearchByIdReq;
import com.gengzp.common.model.generic.resp.Result;
import com.gengzp.common.model.message.req.PageMessageReq;
import com.gengzp.common.model.message.resp.SearchMessageResp;
import com.gengzp.common.model.mq.req.EditTopicReq;
import com.gengzp.common.model.mq.req.RemoveTopicReq;
import com.gengzp.common.model.pagination.resp.PaginationResponse;
import com.gengzp.mq.client.model.message.MqPageMessageResp;
import com.gengzp.mq.client.model.topic.MqTopicResp;
import com.gengzp.permission.annotation.AuthIgnore;
import com.gengzp.permission.annotation.CheckPermission;
import com.gengzp.system.service.MqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName MqController
 * @Description 消息队列控制层
 * @Author gengzp
 * @Date 2025/9/29 18:06
 */
@RestController
@RequestMapping("/mq")
@Api(tags = "消息队列管理模块")
public class MqController {

    @Autowired
    private MqService mqService;

    /**
     * 新增topic
     */
    @PostMapping("/topic/create")
    @ApiOperation("新增topic")
    @CheckPermission("mq:topic:write")
    public Result<Void> createTopic(@Valid @RequestBody EditTopicReq req) {
        mqService.createTopic(req.getTopic(), req.getDescription());
        return Result.getSuccessResult();
    }

    /**
     * 编辑topic描述
     */
    @PostMapping("/topic/edit_description")
    @ApiOperation("编辑topic描述")
    @CheckPermission("mq:topic:write")
    public Result<Void> editTopicDescription(@Valid @RequestBody EditTopicReq req) {
        mqService.editTopicDescription(req.getTopic(), req.getDescription());
        return Result.getSuccessResult();
    }

    /**
     * 删除topic
     */
    @PostMapping("/topic/remove")
    @ApiOperation("删除topic")
    @CheckPermission("mq:topic:write")
    public Result<Void> removeTopic(@Valid @RequestBody RemoveTopicReq req) {
        mqService.removeTopic(req.getTopic());
        return Result.getSuccessResult();
    }

    /**
     * 查询全部topic
     */
    @AuthIgnore
    @OperationLogIgnore
    @PostMapping("/topic/search_all")
    @ApiOperation("查询全部topic")
    public Result<List<MqTopicResp>> searchAllTopic() {
        return Result.getSuccessResult(mqService.searchAllTopic());
    }

    /**
     * 重发消息
     */
    @PostMapping("/resend")
    @ApiOperation("直接重发消息")
    @CheckPermission("mq:resend")
    public Result<Void> resendMessage(@Valid @RequestBody SearchByIdReq req) {
        mqService.resendMessage(req.getId());
        return Result.getSuccessResult();
    }

    /**
     * 根据id查询消息详情
     */
    @PostMapping("/search_by_id")
    @OperationLogIgnore
    @ApiOperation("根据id查询消息详情")
    @CheckPermission("mq:read")
    public Result<SearchMessageResp> searchMessageById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(mqService.searchMessageById(req.getId()));
    }

    /**
     * 分页查询消息
     */
    @PostMapping("/page")
    @OperationLogIgnore
    @ApiOperation("分页查询消息")
    @CheckPermission("mq:read")
    public Result<PaginationResponse<MqPageMessageResp>> pageQueryMqMessages(@Valid @RequestBody PageMessageReq req) {
        return Result.getSuccessResult(mqService.pageQueryMqMessages(req));
    }

}
